package com.zxmr.util;

import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.geom.AffineTransform;
import java.awt.image.BufferedImage;
import java.awt.image.ColorModel;
import java.awt.image.WritableRaster;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.nio.ByteBuffer;
import java.nio.CharBuffer;
import java.nio.charset.Charset;
import java.nio.charset.CharsetDecoder;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import javax.imageio.ImageIO;

import org.apache.commons.compress.archivers.tar.TarArchiveEntry;
import org.apache.commons.compress.archivers.tar.TarArchiveInputStream;
import org.apache.commons.compress.compressors.CompressorInputStream;
import org.apache.commons.compress.compressors.gzip.GzipCompressorInputStream;
import org.apache.commons.io.IOUtils;



public class Utils {

	public static String getCurrentTime()
	{
		SimpleDateFormat dateformat1=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String a1=dateformat1.format(new Date());
		return a1;
	}
	public static boolean isStringEmpty(String str)
	{
		if(str == null||"".equals(str))return true;
		return false;
	}
	public static boolean isStringEmptyWithTrim(String str)
	{
		if(str == null||"".equals(str.trim()))return true;
		return false;
	}
	public static String toUTF(String orign)
	{
		ByteBuffer buffer = ByteBuffer.wrap(orign.getBytes()); 
		Charset charset = null ;
		CharsetDecoder decoder = null ;
		CharBuffer charBuffer = null ;
		try 
		{
			charset = Charset.forName("UTF-8");
			decoder = charset.newDecoder();
			charBuffer = decoder.decode(buffer);
			return  charBuffer.toString().trim();
		} 
		catch (Exception ex)
		{
			ex.printStackTrace();
			return null ;
		} 
	}
	public static void saveImage(InputStream stream,String dest,int width,int height)throws Exception
	{
		BufferedImage source = ImageIO.read(stream);
		
		File saveFile=new File(dest);
		
		int  type = source.getType();  
		BufferedImage target = null ;  
		double  sx = ( double ) width / source.getWidth();  
		double  sy = ( double ) height / source.getHeight();   
		if (sx>sy)  
		{  
			sx = sy;  
			width = (int )(sx * source.getWidth());  
		}else {  
			sy = sx;  
			height = (int )(sy * source.getHeight());  
		}  
		
		if  (type == BufferedImage.TYPE_CUSTOM) {  
			ColorModel cm = source.getColorModel();  
			WritableRaster raster = cm.createCompatibleWritableRaster(width, height);
			boolean  alphaPremultiplied = cm.isAlphaPremultiplied();  
			target = new  BufferedImage(cm, raster, alphaPremultiplied,  null );  
		} else   
		{
			target = new  BufferedImage(width, height, type);  
			Graphics2D g = target.createGraphics();  

			g.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY );  
			g.drawRenderedImage(source, AffineTransform.getScaleInstance(sx, sy));  
			g.dispose();  
			ImageIO.write(target,Utils.getFileFormat(dest),saveFile);
		 }  
	}
		 
	private static String getFileFormat(String filename)
	{
		if(filename == null || filename.isEmpty())return null;
				
		return filename.substring(filename.lastIndexOf('.')+1, filename.length());
	}
	public static long timeFromNow(Date date)
	{
		long now = System.currentTimeMillis();
		return date.getTime() - now;
	}
	
	public static Calendar getResetCalendar()
	{
		Calendar cal = Calendar.getInstance(); 
		cal.set(Calendar.HOUR_OF_DAY, 0); 
		cal.set(Calendar.SECOND, 0); 
		cal.set(Calendar.MINUTE, 0); 
		cal.set(Calendar.MILLISECOND, 0); 
		return cal;
	}
	
	// return the first entry name
	public static String unCompressTar(String inFileName,String outPutPath)throws Exception
	{  
		  final File input = new File(inFileName);
		  final InputStream is = new FileInputStream(input);
		  final CompressorInputStream in = new GzipCompressorInputStream(is);
		  TarArchiveInputStream tin = new TarArchiveInputStream(in);
		  TarArchiveEntry entry = tin.getNextTarEntry();
		  int i = 0;
		  String returnString = null;
		  
		  while (entry != null) {
			  System.out.println(entry.getName());
			  i++;
			  if(i == 1)returnString = entry.getName();
			  
			  File archiveEntry = new File(outPutPath, entry.getName());
			  archiveEntry.getParentFile().mkdirs();
			  if (entry.isDirectory()) {
				  archiveEntry.mkdir();
				  entry = tin.getNextTarEntry();
				  continue;
			  }
			  OutputStream out = new FileOutputStream(archiveEntry);
			  IOUtils.copy(tin, out);
			  out.close();
			  entry = tin.getNextTarEntry();
		  }
		  in.close();
		  tin.close();

		  return returnString;
	}
}
